//
//  XNMainViewController.swift
//  weibo
//
//  Created by 王向宁 on 16/5/19.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit

class XNMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabbar = XNMainTabbar()
        
        mainTabbar.plusBtn.addTarget(self, action: #selector(XNMainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
        
        setValue(mainTabbar, forKey: "tabBar")
        
        addChildViewControllers()
        
    }
    
    @objc private func plusBtnDidClick() {
        print("我被点了")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    private func addChildViewControllers() {
        
        addChildViewController(XNHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        
        addChildViewController(XNDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 1)
        
        addChildViewController(XNMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 2)
        
        addChildViewController(XNProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String, index: Int) {
        
        //标签和导航条的title是一样的话
        vc.title = title
        
        vc.tabBarItem.tag = index
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置title的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        
        //内嵌到导航视图控制器中
        let nav = XNBaseNavController(rootViewController: vc)
        
        //添加子视图控制器
        addChildViewController(nav)
        
    }
    
    //实现协议方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //print(item.tag)
        
        var index = 0
        
        for v in tabBar.subviews {
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    for subView in v.subviews {
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: {
                                subView.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                                    
                            })
                        }
                    }
                }
                
                index += 1
            }
        }
        
    }

   

}
